<div class="layuimini-main">

    <div class="layui-form layuimini-form">
		
		
        <div class="layui-form-item">
            <label class="layui-form-label required">分店名称</label>
            <div class="layui-input-block">
                <input type="text" name="storeName" lay-verify="storeName" placeholder="请输入分店名称" class="layui-input">
			</div>
        </div>
		
		<div class="layui-form-item">
			<label class="layui-form-label required">地址</label>
		    <div class="layui-input-block" id="area-picker1">
				
				<!-- 省市区 -->
				<div class="layui-input-inline" style="width: 150px;">
		        	<select name="province" id="province" class="province-selector" lay-verify="required" data-value="" lay-filter="province-2">
		        		<option value="">请选择省</option>
		        	</select>
		        </div>
		        <div class="layui-input-inline" style="width: 150px;">
		        	<select name="city" id="city" class="city-selector" lay-verify="required" data-value="" lay-filter="city-2">
		        		<option value="">请选择市</option>
		        	</select>
		        </div>
		        <div class="layui-input-inline" style="width: 150px;">
		        	<select name="county" id="county" class="county-selector" lay-verify="required" data-value="" lay-filter="county-2">
		        		<option value="">请选择区</option>
		        	</select>
		        </div>
		    </div>
		</div>
		
		<div class="layui-form-item">
		    <label class="layui-form-label"></label>
		    <div class="layui-input-block">
		        <input type="text" name="address" placeholder="请输入详细地址" autocomplete="off" class="layui-input">
		    </div>
		</div>
		
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>

<script src="lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="lib/jq-module/paigusu.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layarea', 'wangEditor', 'upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
			layarea = layui.layarea,
			wangEditor = layui.wangEditor,
			upload = layui.upload,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
		
		//初始化省市区
		layarea.render({
		    elem: '#area-picker1',
		    change: function (res) {
		        //选择结果
		        console.log(res);
		    }
		});

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
		
		
		
		//自定义验证规则
		form.verify({
			//第一种写法
		    storeName: function (value, item) {	//value控件值，item表单DOM对象
		        if (value.length == 0) {
		            return '用户名不能为空';
		        }
				// if (!new RegExp("^[a-zA-Z0-9_-]{6,16}$").test(value)){	//用户名正则，6到16位（字母，数字，下划线，减号）
				// 	return '用户名必须在6~16位';
				// }
		    }
			//第二种写法
			// password: [/^[a-zA-Z0-9_-]{6,16}$/, '密码必须在6~16位']
			//sex:[/^正则表达式$/, '弹出消息']
			
		});
		
		
		

        //监听提交
        form.on('submit(saveBtn)', function (data) {
			//获得表单数据
			//处理表单数据
			var formData = data.field;
			formData=JSON.parse(JSON.stringify(formData).replace(/province/g,"provinceCode"));
			formData=JSON.parse(JSON.stringify(formData).replace(/city/g,"cityCode"));
			formData=JSON.parse(JSON.stringify(formData).replace(/county/g,"countyCode"));
			formData=JSON.parse(JSON.stringify(formData).replace(/address/g,"addressDetall"));
			formData.province=$("#province").find("option:selected").text();
			formData.city=$("#city").find("option:selected").text();
			formData.county=$("#county").find("option:selected").text();
			formData.parentCode=localStorage.getItem('parentCode');
			// console.log("formData");
			// console.log(formData);
			//通过ajax请求提交
			var index=layer.index;
			$.ajax({
				url : 'http://localhost:8001/sccDrugstore/',
				type: 'post',
				data: JSON.stringify(formData),
				contentType:'application/json;charset=UTF-8',
				success: function(result){
					console.log(result);
					//刷新页面
					table.reload('currentTableId');
					//关闭弹出层
					
				},error:function(result){
					console.log(result);
				}
			});
			layer.close(index);
            return false;
        });

    });
</script>